package com.gooddare.view.component 
{
	import com.gooddare.controller.ApplicationFacade;
	import com.gooddare.event.LyleffEvent;
	import com.gooddare.locator.AssetsLocator;
	import com.gooddare.model.vo.RuleItemVO;
	import com.greensock.TweenMax;
	import com.riatimes.mvc.view.BaseComponent;
	import com.riatimes.util.Reflection;
	import com.riatimes.util.UIUtil;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Lyleff
	 */
	public class RuleItemComponent extends BaseComponent
	{
		private var index:int;
		private var vo:RuleItemVO;
		private var iRule:Sprite;
		private var mmPanel:Sprite;
		private var imgLayer:Sprite;
		private var loader:Loader;
		
		public function RuleItemComponent(index:int,vo:RuleItemVO) 
		{
			this.index = index;
			this.vo = vo;
		}
		override public function initUI () : void {
			iRule = Reflection.createDisplayObjectInstance(AssetsLocator.ASSETS_IRULE_UI) as Sprite;
			this.addChild(iRule);
			mmPanel = UIUtil.getElement(iRule, "mmPanel") as Sprite;
			imgLayer = UIUtil.getElement(iRule, "imgLayer") as Sprite;
			loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, __loaderCompleteEventHandle);
			loader.load(new URLRequest(vo.linkimg));
		}
		
		private function __loaderCompleteEventHandle(e:Event):void 
		{
			var bmpData:BitmapData = new BitmapData(loader.width, loader.height);
			bmpData.draw(loader);
			var bmp:Bitmap = new Bitmap(bmpData);
			bmp.width = 70;
			bmp.height = 60;
			bmp.smoothing = true;
			imgLayer.addChild(bmp);
		}
		override public function initEvent (b:Boolean = true) : void {
			if (b) {
				mmPanel.addEventListener(MouseEvent.CLICK,__mmPanelClickEventHandle)
				mmPanel.addEventListener(MouseEvent.ROLL_OVER, __mmPanelRollEventHandle);
				mmPanel.addEventListener(MouseEvent.ROLL_OUT, __mmPanelRollEventHandle);
			}else {
				mmPanel.removeEventListener(MouseEvent.CLICK,__mmPanelClickEventHandle)
				mmPanel.removeEventListener(MouseEvent.ROLL_OVER, __mmPanelRollEventHandle);
				mmPanel.removeEventListener(MouseEvent.ROLL_OUT, __mmPanelRollEventHandle);
			}
		}
		
		private function __mmPanelClickEventHandle(e:MouseEvent):void 
		{
			ApplicationFacade.getInstance().sendNotification(ApplicationFacade.EVENT_APP_PHOTO_DISPLAY_GET_INDEX, index);
		}
		public function roll(t:int=0):void {
			switch(t) {
				case 0:
					TweenMax.to(imgLayer, 0.2, { y:0 } );
					break;
				case 1:
					TweenMax.to(imgLayer, 0.2, { y: -20 } );
					break;
				case 2:
					TweenMax.to(imgLayer, 0.2, { y: -35 } );
					break;
				case 3:
					TweenMax.to(imgLayer, 0.2, { y: -60 } );
					break;
			}
		}
		private function __mmPanelRollEventHandle(e:MouseEvent):void 
		{
			switch(e.type) {
				case MouseEvent.ROLL_OVER:
					dispatchEvent(new LyleffEvent(LyleffEvent.EVENT_APP_RULEITEM_MOUSE_ROLL_OVER, index));
					break;
				case MouseEvent.ROLL_OUT:
					dispatchEvent(new LyleffEvent(LyleffEvent.EVENT_APP_RULEITEM_MOUSE_ROLL_OUT));
					break;
			}
			
		}
	}

}